package com.cskaoyan.service.impl;

import com.cskaoyan.bean.brand.bo.BrandCreateBO;
import com.cskaoyan.bean.brand.po.BrandPO;
import com.cskaoyan.bean.brand.util.DeletedStateThansform;
import com.cskaoyan.bean.brand.vo.BrandCreateDataVO;
import com.cskaoyan.bean.brand.vo.BrandCreateVO;
import com.cskaoyan.bean.brand.vo.BrandListDataVO;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.po.MarketBrand;
import com.cskaoyan.mapper.BrandMapper;
import com.cskaoyan.service.BrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author chenai
 * @description TODO
 * @date 2022-06-05 14:57
 */
@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    BrandMapper brandMapper;

    @Override
    public BrandListDataVO list(BaseParam baseParam,String id,String name) {
        List<MarketBrand> list = new ArrayList<>();
        List<BrandPO> brandPOList = new ArrayList<>();
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        if(StringUtils.isEmpty(name)){
            name = null;
        }
        if (StringUtils.isEmpty(id)) {
//            brandPOList = brandMapper.select(baseParam.getSort(),baseParam.getOrder(),null,name);
            brandPOList = brandMapper.select(baseParam.getSort(),baseParam.getOrder(),null,name);
        }else {
            brandPOList = brandMapper.select(baseParam.getSort(),baseParam.getOrder(),Integer.parseInt(id),name);
        }

//        }else {
//            brandPOList = brandMapper.select(baseParam.getSort(),baseParam.getOrder(),Integer.parseInt(id),name);
//        }
        for (BrandPO brandPO : brandPOList) {
            list.add(new MarketBrand(brandPO.getId(), brandPO.getName(),brandPO.getDesc(),brandPO.getPicUrl(),brandPO.getSortOrder(),brandPO.getFloorPrice(),brandPO.getAddTime(),brandPO.getUpdateTime(), DeletedStateThansform.POToVO(brandPO.getDeleted())));
        }
        PageInfo pageInfo = new PageInfo(brandPOList);
        BrandListDataVO brandListDataVO = new BrandListDataVO(baseParam.getLimit(),list,baseParam.getPage(),pageInfo.getPages(), ((int) pageInfo.getTotal()));
        return brandListDataVO;
    }

    @Override
    @Transactional
    public BrandCreateDataVO create(BrandCreateBO brandCreateBO) {
        Date date = new Date();
        int id = brandMapper.insertReturnId(brandCreateBO.getDesc(),brandCreateBO.getName(),brandCreateBO.getPicUrl(),brandCreateBO.getFloorPrice(),date);
        BrandCreateDataVO brandCreateDataVO = new BrandCreateDataVO(id, brandCreateBO.getName(), brandCreateBO.getDesc(), brandCreateBO.getPicUrl(), brandCreateBO.getFloorPrice(), date, date);
        return brandCreateDataVO;
    }

    @Override
    public void delete(MarketBrand marketBrand) {
        Date date = new Date();
        //非物理删除，只需要将deleted调整为1
        brandMapper.deleteByPrimaryKey(marketBrand.getId(),date);
    }

    @Override
    public BrandCreateDataVO update(BrandCreateDataVO brandCreateDataVO) {
        Date date = new Date();
        brandMapper.updateByPrimaryKey(brandCreateDataVO.getId(),brandCreateDataVO.getName(),brandCreateDataVO.getDesc(),brandCreateDataVO.getPicUrl(),brandCreateDataVO.getFloorPrice(),date);
        brandCreateDataVO.setUpdateTime(date);
        return brandCreateDataVO;
    }
}
